<template>
    <header>
      <h3>Control Panel</h3>
    </header>
    <p v-if="network">网络状态：<b>连接正常</b></p>
    <p v-else>网络状态：<b style="color: #C40000;">网络错误</b></p>

    <p v-if="card">读卡器状态：<b>连接正常</b></p>
    <p v-else>读卡器状态：<b style="color: #C40000;">连接错误</b></p>
    <a v-if="card" @click="findCard">读取卡号{{ card_no }}</a>

    <p v-if="lock">锁控状态：<b>连接正常</b></p>
    <p v-else>锁控状态：<b style="color: #C40000;">连接错误</b></p>
  </template>
  
  <script>
  
  
  export default {
    data() {
      return {
        network:false, //  网络状态
        card:false, //  读卡器状态
        lock:false, //  锁控状态
        lock_count: 6,
        card_no: ''
      }
    },
    name: 'Menu',
    props: {
      index: ''
    },
    created() {
        window.electron.ipcRenderer.send('check_network','')
        window.electron.ipcRenderer.send('card_init','COM3')
        window.electron.ipcRenderer.send('lock_init','COM8')
    },
    mounted() {
        window.electron.ipcRenderer.on('network_status', (event,val) => {
            this.network = val
        })
        window.electron.ipcRenderer.on('card_status', (event,val) => {
            this.card = val
        })
        window.electron.ipcRenderer.on('card_find_return', (event, val) => {
            this.card_no = this.hex2dec(val.substr(24,8))
        })

        window.electron.ipcRenderer.on('lock_status', (event,val) => {
            this.lock = val
            event.sender.send('lock_send','55 C7 01 00 55')
        })

        window.electron.ipcRenderer.on('lock_info', (event,val) => {
            console.log(val)
        })
    },
    methods: {
        findCard: function () {
            window.electron.ipcRenderer.send('card_find','')
        },

        hex2dec: function(hex) {
            var len = hex.length,
            a = new Array(len),
            code;
            for (var i = 0; i < len; i++) {
                code = hex.charCodeAt(i);
                if (48 <= code && code < 58) {
                code -= 48;
                } else {
                code = (code & 0xdf) - 65 + 10;
                }
                a[i] = code;
            }
            return a.reduce(function (acc, c) {
                acc = 16 * acc + c;
                return acc;
            }, 0);      
            }
    }
  }
  </script>
  
<style  scoped>
  
</style>